﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ivy.SiteAdmin.Entity;
using Ivy.SiteAdmin.DataAccess;
using Ivy.SiteAdmin.IBusinessLogic;

namespace Ivy.SiteAdmin.BusinessLogic
{
    public class SiteAdminContentManagementBus : ISiteAdminContentManagementBus
    {
        #region  Variable & constant
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #region SiteAdminContentManagementBus Members

        #region ContentMetaDataMaster Members
        public bool AddContentMetaData(ContentMetaDataMaster ContentMetaDataMasterEntity)
        {
            bool IsOperationSuccessful = false;

            ContentMetaDataMasterDao lContentMetaDataMasterDao = new ContentMetaDataMasterDao();
            IsOperationSuccessful = lContentMetaDataMasterDao.Insert(ContentMetaDataMasterEntity);

            return IsOperationSuccessful;
        }

        public bool UpdateContentMetaData(ContentMetaDataMaster ContentMetaDataMasterEntity)
        {
            bool IsOperationSuccessful = false;

            ContentMetaDataMasterDao lContentMetaDataMasterDao = new ContentMetaDataMasterDao();
            IsOperationSuccessful = lContentMetaDataMasterDao.Update(ContentMetaDataMasterEntity);

            return IsOperationSuccessful;
        }

        public bool DeleteContentMetaData(ContentMetaDataMaster ContentMetaDataMasterEntity)
        {
            bool IsOperationSuccessful = false;

            ContentMetaDataMasterDao lContentMetaDataMasterDao = new ContentMetaDataMasterDao();
            IsOperationSuccessful = lContentMetaDataMasterDao.Delete(ContentMetaDataMasterEntity);

            return IsOperationSuccessful;
        }

        /// <summary>
        /// This is WRONG it should return the content ststics with metadata
        /// </summary>
        /// <param name="ContentKey"></param>
        /// <returns></returns>
        public ContentMetaDataMaster GetContentSummryByContentKey(int ContentKey)
        {

            ContentMetaDataMasterDao lContentMetaDataMasterDao = new ContentMetaDataMasterDao();
            ContentMetaDataMaster lContentMetaDataMaster;
            lContentMetaDataMaster = lContentMetaDataMasterDao.GetByContentKey(ContentKey);

            if (lContentMetaDataMaster == null)
            {
                return null;
            }
            else
            {
                return lContentMetaDataMaster;
            }
        }

        public ContentMetaDataMaster GetSelectedContentMetaDataByContentCode(string ContentCode)
        {

            ContentMetaDataMasterDao lContentMetaDataMasterDao = new ContentMetaDataMasterDao();
            ContentMetaDataMaster lContentMetaDataMaster;
            lContentMetaDataMaster = lContentMetaDataMasterDao.GetByContentCode(ContentCode);

            if (lContentMetaDataMaster == null)
            {
                return null;
            }
            else
            {
                return lContentMetaDataMaster;
            }
        }

        public ContentMetaDataMaster GetSelectedContentMetaDataByContentName(string ContentName)
        {
            ContentMetaDataMaster lContentMetaDataMaster = new ContentMetaDataMaster();

            ContentMetaDataMasterDao lContentMetaDataMasterDao = new ContentMetaDataMasterDao();
            lContentMetaDataMaster = lContentMetaDataMasterDao.GetByContentName(ContentName);

            return lContentMetaDataMaster;
        }



        public List<ContentMetaDataMaster> GeAllContentMetaDataByContentTypeKey(int ContentTypeKey)
        {
            List<ContentMetaDataMaster> lContentMetaDataMaster = new List<ContentMetaDataMaster>();

            ContentMetaDataMasterDao lContentMetaDataMasterDao = new ContentMetaDataMasterDao();
            lContentMetaDataMaster = lContentMetaDataMasterDao.GetByContentTypeKey(ContentTypeKey);

            return lContentMetaDataMaster;
        }

        public int SaveContentLikes(int ContentKey)
        {
            int lContentTotalLikes = -1;

            ContentMetaDataMaster lContentMetaDataMaster;
            ContentMetaDataMasterDao lContentMetaDataMasterDao = new ContentMetaDataMasterDao();
            lContentMetaDataMaster = lContentMetaDataMasterDao.GetByContentKey(ContentKey);

            if (lContentMetaDataMaster != null)
            {
                lContentTotalLikes = lContentMetaDataMaster.ContentTotalLikes;
            }

            return lContentTotalLikes;

        }
        #endregion

        #region ContentTypeMaster Members
        public bool AddContentType(ContentTypeMaster ContentTypeMasterEntity)
        {
            bool IsOperationSuccessful = false;
            ContentTypeMasterDao lContentTypeMasterDao = new ContentTypeMasterDao();
            IsOperationSuccessful = lContentTypeMasterDao.Insert(ContentTypeMasterEntity);

            return IsOperationSuccessful;
        }

        public ContentTypeMaster GetSelectedContentTypeByKey(int ContentTypeKey)
        {

            ContentTypeMasterDao lContentTypeMasterDao = new ContentTypeMasterDao();
            ContentTypeMaster lContentTypeMaster;
            lContentTypeMaster = lContentTypeMasterDao.GetByContentTypeKey(ContentTypeKey);

            if (lContentTypeMaster == null)
            {
                return null;
            }
            else
            {
                return lContentTypeMaster;
            }

        }

        public int GetContentTypeKeyByContentTypeCode(string ContentTypeCode)
        {
            int lContentTypeKey = -1;
            ContentTypeMasterDao lContentTypeMasterDao = new ContentTypeMasterDao();
            ContentTypeMaster lContentTypeMaster;
            lContentTypeMaster = lContentTypeMasterDao.GetByContentTypeCode(ContentTypeCode);

            if (lContentTypeMaster != null)
            {
                lContentTypeKey = lContentTypeMaster.ContentTypeKey;

            }

            return lContentTypeKey;
        }

        public List<ContentTypeMaster> GetSelectedContentTypeByName(string ContentTypeName)
        {
            List<ContentTypeMaster> lContentTypeMaster = new List<ContentTypeMaster>();

            ContentTypeMasterDao lContentTypeMasterDao = new ContentTypeMasterDao();
            lContentTypeMaster = lContentTypeMasterDao.GetByContentTypeName(ContentTypeName);

            return lContentTypeMaster;
        }

        #endregion

        #region ContentFeedback Members
        public bool RegisterNewContentFeedback(ContentFeedback ContentFeedbackEntity)
        {
            bool IsOperationSuccessful = false;

            ContentFeedbackDao lContentFeedbackDao = new ContentFeedbackDao();
            IsOperationSuccessful = lContentFeedbackDao.Insert(ContentFeedbackEntity);

            return IsOperationSuccessful;
        }

        public bool UpdateRegisterdContentFeedback(ContentFeedback ContentFeedbackEntity)
        {
            bool IsOperationSuccessful = false;

            ContentFeedbackDao lContentFeedbackDao = new ContentFeedbackDao();
            IsOperationSuccessful = lContentFeedbackDao.Update(ContentFeedbackEntity);

            return IsOperationSuccessful;
        }

        public List<ContentFeedback> GetSeletedContentFeedbackByContentKey(int ContentFeedbackContentKey)
        {
            List<ContentFeedback> lContentFeedback = new List<ContentFeedback>();

            ContentFeedbackDao lContentFeedbackDao = new ContentFeedbackDao();
            lContentFeedback = lContentFeedbackDao.GetByContentFeedbackContentKey(ContentFeedbackContentKey);

            return lContentFeedback;
        }

        public List<ContentFeedback> GetSelectedContentFeedbackByFeedbackKey(int ContentFeedbackFeedbackKey)
        {
            List<ContentFeedback> lContentFeedback = new List<ContentFeedback>();

            ContentFeedbackDao lContentFeedbackDao = new ContentFeedbackDao();
            lContentFeedback = lContentFeedbackDao.GetByContentFeedbackFeedbackKey(ContentFeedbackFeedbackKey);

            return lContentFeedback;
        }

        public List<ContentFeedbacks> GetSelectedContentFeedbackByContentCode(string ContentCode)
        {
            List<ContentFeedbacks> lContentFeedbackList = new List<ContentFeedbacks>();

            ContentFeedbackDao lContentFeedbackDao = new ContentFeedbackDao();
            lContentFeedbackList = lContentFeedbackDao.GetAllContentFeedbackByContentCode(ContentCode);

            return lContentFeedbackList;
        }

        public List<ContentFeedback> GetAllContentFeedback()
        {
            List<ContentFeedback> lContentFeedbackList = new List<ContentFeedback>();

            ContentFeedbackDao lContentFeedbackDao = new ContentFeedbackDao();
            lContentFeedbackList = lContentFeedbackDao.GetAll();

            return lContentFeedbackList;
        }

        #endregion

        #region ContentRating Members
        //What is this one?????????
        //bool Insert(ContentRating ContentRatingEntity)
        public bool SaveContentVotesRating(ContentRating ContentRatingEntity)
        {
            bool IsOperationSuccessful = false;

            ContentRatingDao lContentRatingDao = new ContentRatingDao();
            IsOperationSuccessful = lContentRatingDao.Insert(ContentRatingEntity);

            return IsOperationSuccessful;
        }

        public List<ContentRating> GetContentRatingByContentKey(int ContentKey)
        {
            List<ContentRating> lContentFeedbackList = new List<ContentRating>();

            ContentRatingDao lContentRatingDao = new ContentRatingDao();
            lContentFeedbackList = lContentRatingDao.GetByContentKey(ContentKey);

            return lContentFeedbackList;
        }
        #endregion

        #region ContentStatistics Members

        public List<ContentStatistics> GetAllContentMetaDataWithContentStatistics(string ContentTypeCode)
        {
            List<ContentStatistics> lContentList = new List<ContentStatistics>();
            //TO BE COMPLETED ....
            int ContentTypeKey = GetContentTypeKeyByContentTypeCode(ContentTypeCode);
            lContentList = GetAllContentMetaDataWithContentStatistics(ContentTypeKey);
            return lContentList;
        }
        public List<ContentStatistics> GetAllContentMetaDataWithContentStatistics(int ContentTypeKey)
        {
            List<ContentStatistics> lContentList = new List<ContentStatistics>();
            ContentMetaDataMasterDao lContentMetaDataMasterDao = new ContentMetaDataMasterDao();
            lContentList = lContentMetaDataMasterDao.GetAllContentStatisticsWithContentTypeKey(ContentTypeKey);

            return lContentList;
        }

        public ContentStatistics GetSelectedContentMetaDataWithContentStatisticsByContentkey(int ContentKey)
        {
            ContentStatistics lContentStatisticsList = new ContentStatistics();
            ContentMetaDataMasterDao lContentMetaDataMasterDao = new ContentMetaDataMasterDao();
            lContentStatisticsList = lContentMetaDataMasterDao.GetContentStatisticsWithContneKey(ContentKey);

            if (lContentStatisticsList == null)
            {
                return null;
            }
            else
            {
                return lContentStatisticsList;
            }

        }
        #endregion

        #region Sys_ErrorLog Members
        public bool SaveSys_ErrorLogDetails(Sys_ErrorLog Sys_ErrorLogEntity)
        {
            bool IsOperationSuccessful = false;

            Sys_ErrorLogDao lSys_ErrorLogDao = new Sys_ErrorLogDao();
            IsOperationSuccessful = lSys_ErrorLogDao.Insert(Sys_ErrorLogEntity);

            return IsOperationSuccessful;
        }

        public Sys_ErrorLog GetSelectedSys_ErrorLogByErrorLogPrimaryID(int ErrorLogPrimaryID)
        {

            Sys_ErrorLogDao lSys_ErrorLogDao = new Sys_ErrorLogDao();
            Sys_ErrorLog lSys_ErrorLog;
            lSys_ErrorLog = lSys_ErrorLogDao.GetByErrorLogPrimaryKey(ErrorLogPrimaryID);

            if (lSys_ErrorLog == null)
            {
                return null;
            }
            else
            {
                return lSys_ErrorLog;
            }

        }

        public List<Sys_ErrorLog> GetAllSys_ErrorLogDetails()
        {
            List<Sys_ErrorLog> lSys_ErrorLogList = new List<Sys_ErrorLog>();
            Sys_ErrorLogDao lSys_ErrorLogDao = new Sys_ErrorLogDao();
            lSys_ErrorLogList = lSys_ErrorLogDao.GetAll();

            return lSys_ErrorLogList;
        }
        #endregion

        #region ContentBinaryDataContent Members
        public bool SaveContentBinaryData(ContentBinaryDataContent ContentBinaryDataContentEntity)
        {
            bool IsOperationSuccessful = false;
            if (ContentBinaryDataContentEntity.ContentMetaDataKey > 0)
            {
                if (ContentBinaryDataContentEntity.ContentBinaryData != null)
                {
                    ContentBinaryDataContentDao lContentBinaryDataContentDao = new ContentBinaryDataContentDao();
                    ContentBinaryDataContent lContentBinaryDataContent = lContentBinaryDataContentDao.GetByContentMetaDataKey(ContentBinaryDataContentEntity.ContentMetaDataKey);
                    if (lContentBinaryDataContent != null)
                    {
                        //Update existing record
                        IsOperationSuccessful = lContentBinaryDataContentDao.Update(ContentBinaryDataContentEntity);
                    }
                    else
                    {
                        //Insert new record
                        IsOperationSuccessful = lContentBinaryDataContentDao.Insert(ContentBinaryDataContentEntity);
                    }
                }
            }
            return IsOperationSuccessful;
        }

        //public bool UpdateContentBinaryData(ContentBinaryDataContent ContentBinaryDataContentEntity)
        //{
        //    bool IsOperationSuccessful = false;

        //    ContentBinaryDataContentDao lContentBinaryDataContentDao = new ContentBinaryDataContentDao();
        //    IsOperationSuccessful = lContentBinaryDataContentDao.Update(ContentBinaryDataContentEntity);

        //    return IsOperationSuccessful;
        //}

        public ContentBinaryDataContent GetSelectedContentBinaryDataByKey(int ContentMetaDataKey)
        {
            ContentBinaryDataContent lContentBinaryDataContent = new ContentBinaryDataContent();
            ContentBinaryDataContentDao lContentBinaryDataContentDao = new ContentBinaryDataContentDao();
            lContentBinaryDataContent = lContentBinaryDataContentDao.GetByContentMetaDataKey(ContentMetaDataKey);

            if (lContentBinaryDataContent == null)
            {
                return null;
            }
            else
            {
                return lContentBinaryDataContent;
            }
        }

        #endregion


        #endregion
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        #endregion Private Methods
    }
}
